﻿#include <QStyleFactory>
#include <QFile>
#include <QMessageBox>
#include "CTemplateOperDlg.h"
#include "CHeader.h"
#include "CLogOper.h"
#include "CSqlOper.h"
CTemplateOperDlg::CTemplateOperDlg(QString sSceneGuid, int nActOrder, CRBaseData* pBasicData, QWidget *parent)
	: m_sGuid(sSceneGuid), m_nActOrder(nActOrder),m_pBasicData(pBasicData),QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	m_sTemplateFixGuid = "";
	ui.labelTemplateGuid->hide();
	ui.lineEditTemplateGuid->hide();
	setStyleSheetFile();
	initUi();
	initConnect();
	refushTemplateList();
}

CTemplateOperDlg::~CTemplateOperDlg()
{}

void CTemplateOperDlg::refushDataByTemplateID(QString sTemplateGuid)
{
	//首先先将界面中所有的数据清空
	ui.lineEditRunSpeed->clear();
	ui.lineEditSpeedZ->clear();
	ui.lineEditTemplateName->clear();
	ui.lineEditTemplateGuid->setText(sTemplateGuid);

	//首先拿到模版中的数据
	QMap<QString, CTemplateInfo*> mapTemplateInfo;
	QString sErrMsg = "";
	selectTemplateBySceneID(m_sGuid, mapTemplateInfo, sErrMsg);
	CTemplateInfo* pTemplateInfo = mapTemplateInfo[sTemplateGuid];
	QString sTemplateName = pTemplateInfo->m_sTemplateName;
	double dPollSpeed = pTemplateInfo->m_dPollSpeed;
	double dSpeedZ = pTemplateInfo->m_dSpeedZ;
	int nActOrder = pTemplateInfo->m_nActOrder;
	QVector<QString> vecItemGuid = pTemplateInfo->m_vecItemID;
	QVector<QString> vecItemOrder = pTemplateInfo->m_vecItemOrder;
	QVector<QString> vecItemGuidOrder;
	if (vecItemGuid.size() != vecItemOrder.size())//默认是显示为正向
	{
		nActOrder = 0;
		for (int i = 0; i < vecItemGuid.size(); i++)
		{
			vecItemGuidOrder.push_back(vecItemGuid[i] + "-" + "1");
		}
	}
	else
	{
		//将对应的顺序记住，防止后边排序以后顺序打乱
		for (int i = 0; i < vecItemGuid.size(); i++)
		{
			vecItemGuidOrder.push_back(vecItemGuid[i] + "-" + vecItemOrder[i]);
		}
	}

	//然后修改界面
	ui.lineEditRunSpeed->setText(QString::number(dPollSpeed));
	ui.lineEditSpeedZ->setText(QString::number(dSpeedZ));
	ui.lineEditTemplateName->setText(sTemplateName);
	ui.comboBoxActOrder->setCurrentText(m_mapActOrder.value(nActOrder));

	//模版信息树补全
	if (m_pTreeInfoModel != nullptr)
	{
		for (int i = 0; i < m_pTreeInfoModel->rowCount(); i++)
		{
			QStandardItem *pItem = m_pTreeInfoModel->item(i);

			for (int j = 0; j < pItem->rowCount(); j++)
			{
				QStandardItem *SubpItem = pItem->child(i);
				delete SubpItem;
				SubpItem = nullptr;
			}
			pItem->setRowCount(0);
			delete pItem;
			pItem = nullptr;
		}
		m_pTreeInfoModel->setRowCount(0);
	}
	int nIndex = 0;
	if (nActOrder == 0)//正向
	{
		//拿到巡检点数组
		QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
		QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
		qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);
		for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
		{
			QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
			pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
			pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
			m_pTreeInfoModel->setItem(nIndex, 0, pItem);

			int k = 0;
			QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
			QVector<CPolItem*> vecItem = mapItem.values().toVector();
			qSort(vecItem.begin(), vecItem.end(), sortPolItem);
			for (int j = 0; j < vecItem.size(); j++)
			{
				CPolItem* pPolItem = vecItem[j];
				if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
				{
					QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
					folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
					folder->setData(pPolItem->m_sItemGuid, 300);
					folder->setData(QString::number(1), 301);
					folder->setCheckable(true);
					if (vecItemGuid.contains(pPolItem->m_sItemGuid)&&vecItemGuidOrder.contains(pPolItem->m_sItemGuid+"-1"))
					{
						folder->setCheckState(Qt::Checked);
					}
					m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
					k++;
				}
			}
		}
	}
	else if (nActOrder == 1)//反向
	{
		//拿到巡检点数组
		QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
		QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
		qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPointReverse);
		for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
		{
			QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
			pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
			pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
			m_pTreeInfoModel->setItem(nIndex, 0, pItem);

			int k = 0;
			QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
			QVector<CPolItem*> vecItem = mapItem.values().toVector();
			qSort(vecItem.begin(), vecItem.end(), sortPolItem);
			for (int j = 0; j < vecItem.size(); j++)
			{
				CPolItem* pPolItem = vecItem[j];
				if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
				{
					QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
					folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
					folder->setData(pPolItem->m_sItemGuid, 300);
					folder->setData(QString::number(2), 301);
					folder->setCheckable(true);
					if (vecItemGuid.contains(pPolItem->m_sItemGuid) && vecItemGuidOrder.contains(pPolItem->m_sItemGuid + "-2"))
					{
						folder->setCheckState(Qt::Checked);
					}
					m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
					k++;
				}
			}
		}
	}
	else if (nActOrder == 2)//双向
	{
		{
			//拿到巡检点数组
			QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
			QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
			qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);
			for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
			{
				QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
				pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
				pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
				m_pTreeInfoModel->setItem(nIndex, 0, pItem);

				int k = 0;
				QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
				QVector<CPolItem*> vecItem = mapItem.values().toVector();
				qSort(vecItem.begin(), vecItem.end(), sortPolItem);
				for (int j = 0; j < vecItem.size(); j++)
				{
					CPolItem* pPolItem = vecItem[j];
					if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
					{
						QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
						folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
						folder->setData(pPolItem->m_sItemGuid, 300);
						folder->setData(QString::number(1), 301);
						folder->setCheckable(true);
						if (vecItemGuid.contains(pPolItem->m_sItemGuid) && vecItemGuidOrder.contains(pPolItem->m_sItemGuid + "-1"))
						{
							folder->setCheckState(Qt::Checked);
						}
						m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
						k++;
					}
				}
			}
		}
		
		{
			//拿到巡检点数组
			QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
			QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
			qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPointReverse);
			for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
			{
				QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
				pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
				pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
				m_pTreeInfoModel->setItem(nIndex, 0, pItem);

				int k = 0;
				QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
				QVector<CPolItem*> vecItem = mapItem.values().toVector();
				qSort(vecItem.begin(), vecItem.end(), sortPolItem);
				for (int j = 0; j < vecItem.size(); j++)
				{
					CPolItem* pPolItem = vecItem[j];
					if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
					{
						QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
						folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
						folder->setData(pPolItem->m_sItemGuid, 300);
						folder->setData(QString::number(2), 301);
						folder->setCheckable(true);
						if (vecItemGuid.contains(pPolItem->m_sItemGuid) && vecItemGuidOrder.contains(pPolItem->m_sItemGuid + "-2"))
						{
							folder->setCheckState(Qt::Checked);
						}
						m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
						k++;
					}
				}
			}
		}
	}
	else if (nActOrder == 3)//双向
	{
		{
			//拿到巡检点数组
			QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
			QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
			qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPointReverse);
			for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
			{
				QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
				pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
				pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
				m_pTreeInfoModel->setItem(nIndex, 0, pItem);

				int k = 0;
				QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
				QVector<CPolItem*> vecItem = mapItem.values().toVector();
				qSort(vecItem.begin(), vecItem.end(), sortPolItem);
				for (int j = 0; j < vecItem.size(); j++)
				{
					CPolItem* pPolItem = vecItem[j];
					if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
					{
						QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
						folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
						folder->setData(pPolItem->m_sItemGuid, 300);
						folder->setData(QString::number(2), 301);
						folder->setCheckable(true);
						if (vecItemGuid.contains(pPolItem->m_sItemGuid) && vecItemGuidOrder.contains(pPolItem->m_sItemGuid + "-2"))
						{
							folder->setCheckState(Qt::Checked);
						}
						m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
						k++;
					}
				}
			}
		}

		{
			//拿到巡检点数组
			QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
			QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
			qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);
			for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
			{
				QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
				pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
				pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
				m_pTreeInfoModel->setItem(nIndex, 0, pItem);

				int k = 0;
				QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
				QVector<CPolItem*> vecItem = mapItem.values().toVector();
				qSort(vecItem.begin(), vecItem.end(), sortPolItem);
				for (int j = 0; j < vecItem.size(); j++)
				{
					CPolItem* pPolItem = vecItem[j];
					if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
					{
						QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
						folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
						folder->setData(pPolItem->m_sItemGuid, 300);
						folder->setData(QString::number(1), 301);
						folder->setCheckable(true);
						if (vecItemGuid.contains(pPolItem->m_sItemGuid) && vecItemGuidOrder.contains(pPolItem->m_sItemGuid + "-1"))
						{
							folder->setCheckState(Qt::Checked);
						}
						m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
						k++;
					}
				}
			}
		}
	}
	ui.treeViewInfo->expandAll();
}

void CTemplateOperDlg::setStyleSheetFile()
{
	//设置对话框标题
	this->setWindowTitle(A2T("模版管理"));
	QString sQss = setUiStyle("page");
	QString sMyqss = styleSheet();
	QString sMerged = sQss + "\n" + sMyqss; // 后面的会覆盖前面的相同规则	
	setStyleSheet(sMerged);
}

void CTemplateOperDlg::initConnect()
{
	// 连接信号和槽
	connect(ui.comboBoxActOrder, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CTemplateOperDlg::onIndexChanged);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CTemplateOperDlg::slotClose);
	connect(ui.pushButtonAllCheck, &QPushButton::clicked, this, &CTemplateOperDlg::slotPushButtonAllCheckClicked);
	connect(ui.pushButtonDisAllCheck, &QPushButton::clicked, this, &CTemplateOperDlg::slotPushButtonDisAllCheckedClicked);
	connect(ui.pushButtonExpandInfo, &QPushButton::clicked, this, &CTemplateOperDlg::slotExpandInfo);
	connect(ui.pushButtonShrinkInfo, &QPushButton::clicked, this, &CTemplateOperDlg::slotShrinkInfo);
	connect(ui.pushButtonSave, &QPushButton::clicked, this, &CTemplateOperDlg::slotPushButtonSave);
}

void CTemplateOperDlg::initUi()
{
	m_mapActOrder.insert(0, A2T("顺序执行"));
	m_mapActOrder.insert(1, A2T("反向执行"));
	m_mapActOrder.insert(2, A2T("双向执行"));
	m_mapActOrder.insert(3, A2T("倒序双向执行"));
	ui.comboBoxActOrder->addItem(A2T("顺序执行"));
	ui.comboBoxActOrder->addItem(A2T("反向执行"));
	ui.comboBoxActOrder->addItem(A2T("双向执行"));
	ui.comboBoxActOrder->addItem(A2T("倒序双向执行"));
	m_pTreeInfoModel = new QStandardItemModel();
	ui.treeViewInfo->setModel(m_pTreeInfoModel);
	ui.treeViewInfo->setStyle(QStyleFactory::create("windows"));
	ui.treeViewInfo->setSelectionMode(QAbstractItemView::SingleSelection);
	ui.treeViewInfo->setFrameShape(QFrame::NoFrame);
	ui.treeViewInfo->header()->hide();
	ui.treeViewInfo->setContextMenuPolicy(Qt::CustomContextMenu);
	ui.treeViewInfo->setEditTriggers(QAbstractItemView::NoEditTriggers);//不可编辑
	ui.treeViewInfo->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	ui.treeViewInfo->setFocusPolicy(Qt::NoFocus);
}

void CTemplateOperDlg::refushTemplateList()
{
	//巡检点更新到树中
	if (m_pTreeInfoModel != nullptr)
	{
		for (int i = 0; i < m_pTreeInfoModel->rowCount(); i++)
		{
			QStandardItem *pItem = m_pTreeInfoModel->item(i);

			for (int j = 0; j < pItem->rowCount(); j++)
			{
				QStandardItem *SubpItem = pItem->child(i);
				delete SubpItem;
				SubpItem = nullptr;
			}
			pItem->setRowCount(0);
			delete pItem;
			pItem = nullptr;
		}
		m_pTreeInfoModel->setRowCount(0);
	}
	int nIndex = 0;

	//首先判断是正向、反向、双向执行
	if (m_nActOrder == 0)//顺序执行
	{
		//拿到巡检点数组
		QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
		QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
		qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);
		for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
		{
			QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
			pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
			pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
			m_pTreeInfoModel->setItem(nIndex, 0, pItem);

			int k = 0;
			QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
			QVector<CPolItem*> vecItem = mapItem.values().toVector();
			qSort(vecItem.begin(), vecItem.end(), sortPolItem);
			for (int j = 0; j < vecItem.size(); j++)
			{
				CPolItem* pPolItem = vecItem[j];
				if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
				{
					QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
					folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
					folder->setData(pPolItem->m_sItemGuid, 300);
					folder->setData(QString::number(1), 301);
					folder->setCheckable(true);
					m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
					k++;
				}
			}
		}
	}
	else if (m_nActOrder == 1)//反向执行 
	{
		//拿到巡检点数组
		QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
		QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
		qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPointReverse);//反向排序
		for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
		{
			QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
			pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
			pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
			m_pTreeInfoModel->setItem(nIndex, 0, pItem);

			int k = 0;
			QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
			QVector<CPolItem*> vecItem = mapItem.values().toVector();
			qSort(vecItem.begin(), vecItem.end(), sortPolItem);
			for (int j = 0; j < vecItem.size(); j++)
			{
				CPolItem* pPolItem = vecItem[j];
				if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
				{
					QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
					folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
					folder->setData(pPolItem->m_sItemGuid, 300);
					folder->setData(QString::number(2), 301);
					folder->setCheckable(true);
					m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
					k++;
				}
			}
		}
	}
	else if (m_nActOrder == 2)//双向执行  正向和反向各来一遍
	{
		{
			//拿到巡检点数组
			QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
			QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
			qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);//反向排序
			for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
			{
				QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
				pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
				pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
				m_pTreeInfoModel->setItem(nIndex, 0, pItem);

				int k = 0;
				QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
				QVector<CPolItem*> vecItem = mapItem.values().toVector();
				qSort(vecItem.begin(), vecItem.end(), sortPolItem);
				for (int j = 0; j < vecItem.size(); j++)
				{
					CPolItem* pPolItem = vecItem[j];
					if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
					{
						QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
						folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
						folder->setData(pPolItem->m_sItemGuid, 300);
						folder->setData(QString::number(1), 301);
						folder->setCheckable(true);
						m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
						k++;
					}
				}
			}
		}

		{
			//拿到巡检点数组
			QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
			QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
			qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPointReverse);//反向排序
			for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
			{
				QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
				pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
				pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
				m_pTreeInfoModel->setItem(nIndex, 0, pItem);

				int k = 0;
				QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
				QVector<CPolItem*> vecItem = mapItem.values().toVector();
				qSort(vecItem.begin(), vecItem.end(), sortPolItem);
				for (int j = 0; j < vecItem.size(); j++)
				{
					CPolItem* pPolItem = vecItem[j];
					if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
					{
						QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
						folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
						folder->setData(pPolItem->m_sItemGuid, 300);
						folder->setData(QString::number(2), 301);
						folder->setCheckable(true);
						m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
						k++;
					}
				}
			}
		}
	}
	else if (m_nActOrder == 3)
	{
		{
			//拿到巡检点数组
			QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
			QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
			qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPointReverse);//反向排序
			for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
			{
				QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
				pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
				pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
				m_pTreeInfoModel->setItem(nIndex, 0, pItem);

				int k = 0;
				QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
				QVector<CPolItem*> vecItem = mapItem.values().toVector();
				qSort(vecItem.begin(), vecItem.end(), sortPolItem);
				for (int j = 0; j < vecItem.size(); j++)
				{
					CPolItem* pPolItem = vecItem[j];
					if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
					{
						QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
						folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
						folder->setData(pPolItem->m_sItemGuid, 300);
						folder->setData(QString::number(2), 301);
						folder->setCheckable(true);
						m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
						k++;
					}
				}
			}
		}

		{
			//拿到巡检点数组
			QMap<QString, CPolPoint*> mapPolPoint = m_pBasicData->m_pPolArea->m_mapPolPoint;
			QVector<CPolPoint*> vecPolPoint = mapPolPoint.values().toVector();
			qSort(vecPolPoint.begin(), vecPolPoint.end(), sortPoint);//正向排序
			for (int i = 0; i < vecPolPoint.size(); i++, nIndex++)
			{
				QStandardItem *pItem = new QStandardItem(vecPolPoint[i]->m_sPointName);
				pItem->setIcon(QIcon(":/icon/Rob/Point.png"));
				pItem->setData(vecPolPoint[i]->m_sPointGuid, 300);
				m_pTreeInfoModel->setItem(nIndex, 0, pItem);

				int k = 0;
				QMap<QString, CPolItem*> mapItem = m_pBasicData->m_pPolArea->m_mapPolPoint[vecPolPoint[i]->m_sPointGuid]->m_mapPolItem;
				QVector<CPolItem*> vecItem = mapItem.values().toVector();
				qSort(vecItem.begin(), vecItem.end(), sortPolItem);
				for (int j = 0; j < vecItem.size(); j++)
				{
					CPolItem* pPolItem = vecItem[j];
					if (pPolItem->m_sPointGuid == vecPolPoint[i]->m_sPointGuid)
					{
						QStandardItem* folder = new QStandardItem(pPolItem->m_sItemName);
						folder->setIcon(QIcon(":/icon/Rob/TaskMode.png"));
						folder->setData(pPolItem->m_sItemGuid, 300);
						folder->setData(QString::number(1), 301);
						folder->setCheckable(true);
						m_pTreeInfoModel->item(nIndex, 0)->setChild(k, 0, folder);
						k++;
					}
				}
			}
		}
	}
	
	ui.treeViewInfo->expandAll();
}

void CTemplateOperDlg::onIndexChanged(int index)
{
	m_nActOrder = m_mapActOrder.key(ui.comboBoxActOrder->currentText());
	refushTemplateList();
}

void CTemplateOperDlg::slotPushButtonSave()
{
	//提醒一下
	int nRet = messageBox(3, A2T("是否保存此模版"));
	if (QMessageBox::No == nRet || QMessageBox::Close == nRet)
	{
		return;
	}
	if (m_nType == 0)
	{
		//检查数据的合法性
		if (ui.lineEditTemplateName->text() == "" || ui.lineEditSpeedZ->text() == "" || ui.lineEditRunSpeed->text() == "")
		{
			messageBox(0, A2T("请填写完整后再进行操作"));
			return;
		}
		//看看模版的名称是否重复
		QMap<QString, CTemplateInfo*> mapTemplateInfo;
		QString sErrMsg = "";
		selectTemplateBySceneID(m_sGuid, mapTemplateInfo, sErrMsg);
		for (QMap<QString, CTemplateInfo*>::iterator itor = mapTemplateInfo.begin(); itor != mapTemplateInfo.end(); itor++)
		{
			CTemplateInfo* pTemplateInfo = itor.value();
			if (ui.lineEditTemplateName->text() == pTemplateInfo->m_sTemplateName)
			{
				messageBox(0, A2T("已经存在名称为%1的模版，不能重复添加.").arg(ui.lineEditTemplateName->text()));
				return;
			}
		}

		CTemplateInfo templateInfo;
		//遍历树，将打钩存起来
		QVector<QString> vecItem;
		QVector<QString> vecOrder;
		// 遍历并取消勾选所有二级节点
		for (int i = 0; i < m_pTreeInfoModel->invisibleRootItem()->rowCount(); ++i)
		{
			QStandardItem *item = m_pTreeInfoModel->invisibleRootItem()->child(i);
			for (int j = 0; j < item->rowCount(); j++)
			{
				QStandardItem *pChild = item->child(j);
				//勾选了
				if (pChild->checkState() == Qt::Checked)
				{
					vecItem.push_back(pChild->data(300).toString());
					vecOrder.push_back(pChild->data(301).toString());
				}
			}
		}
		//执行sql语句
		QString sTemplateName = ui.lineEditTemplateName->text();
		double dRobSpeed = ui.lineEditRunSpeed->text().toDouble();
		double dSpeedZ = ui.lineEditSpeedZ->text().toDouble();
		QString sGuid = createGuid();
		QString sFixedGuid = sGuid;
		templateInfo.m_vecItemID = vecItem;
		templateInfo.m_vecItemOrder = vecOrder;
		templateInfo.m_sTemplateGuid = sGuid;
		templateInfo.m_sTemplateFixedID = sFixedGuid;
		templateInfo.m_dPollSpeed = dRobSpeed;
		templateInfo.m_dSpeedZ = dSpeedZ;
		templateInfo.m_nActOrder = m_mapActOrder.key(ui.comboBoxActOrder->currentText());
		templateInfo.m_nLatestRecord = 1;//是最新的数据
		templateInfo.m_sGuid = m_sGuid;
		templateInfo.m_sTemplateName = sTemplateName;
		templateInfo.m_nItemCount = templateInfo.m_vecItemID.size();
		templateInfo.m_sUpdateTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
		bool bRet = addTemplate(templateInfo, sErrMsg);
		if (bRet == false)
		{
			SExecLog log;
			log.m_nAlarmLevel = enSysAlarm;
			log.m_sFuncName = A2T("数据库");
			log.m_sDescriptionl = sErrMsg;
			CLogOper::instance().addMsg(log,m_sGuid);
			messageBox(0, A2T("添加失败"));
			return;
		}
		//刷新界面
		messageBox(0, A2T("添加成功"));
		SExecLog sLog;
		sLog.m_nAlarmLevel = enWorkInfo;
		sLog.m_sDescriptionl = sTemplateName;
		/*sLog.m_sDescriptionl = A2T("%1添加了%2名为%3的模版")
			.arg(CPublicData::instance().m_userInfo.sUserName)
			.arg(CPublicData::instance().m_mapIFScene[m_sGuid]->m_sSceneName.get())
			.arg(sTemplateName);*/
		sLog.m_sFuncName = A2T("模版");
		CLogOper::instance().addMsg(sLog,m_sGuid);
		this->close();
	}
	else if (m_nType == 1)
	{
		QString sTemplateFixedId = ui.lineEditTemplateGuid->text();
		QString sErrMsg = "";
		//首先要将之前的记录设置成不是最新记录
		setTemplateOldByFixedId(sTemplateFixedId, sErrMsg);//执行删除模版函数

		//将此模版增加一个新的记录
		CTemplateInfo templateInfo;
		//遍历树，将打钩存起来
		QVector<QString> vecItem;
		QVector<QString> vecOrder;
		// 遍历所有勾选的二级节点
		for (int i = 0; i < m_pTreeInfoModel->invisibleRootItem()->rowCount(); ++i)
		{
			QStandardItem *item = m_pTreeInfoModel->invisibleRootItem()->child(i);
			for (int j = 0; j < item->rowCount(); j++)
			{
				QStandardItem *pChild = item->child(j);
				//勾选了
				if (pChild->checkState() == Qt::Checked)
				{
					vecItem.push_back(pChild->data(300).toString());
					vecOrder.push_back(pChild->data(301).toString());
				}
			}
		}

		//执行sql语句
		QString sTemplateName = ui.lineEditTemplateName->text();
		double dRobSpeed = ui.lineEditRunSpeed->text().toDouble();
		double dSpeedZ = ui.lineEditSpeedZ->text().toDouble();
		QString sGuid = createGuid();
		QString sFixedGuid = sTemplateFixedId;
		templateInfo.m_vecItemID = vecItem;
		templateInfo.m_vecItemOrder = vecOrder;
		templateInfo.m_sTemplateGuid = sGuid;
		templateInfo.m_sTemplateFixedID = sFixedGuid;
		templateInfo.m_dPollSpeed = dRobSpeed;
		templateInfo.m_dSpeedZ = dSpeedZ;
		templateInfo.m_nActOrder = m_mapActOrder.key(ui.comboBoxActOrder->currentText());
		templateInfo.m_nLatestRecord = 1;//是最新的数据
		templateInfo.m_sGuid = m_sGuid;
		templateInfo.m_sTemplateName = sTemplateName;
		templateInfo.m_nItemCount = templateInfo.m_vecItemID.size();
		templateInfo.m_sUpdateTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
		bool bRet = addTemplate(templateInfo, sErrMsg);
		if (bRet == false)
		{
			SExecLog log;
			log.m_nAlarmLevel = enSysAlarm;
			log.m_sFuncName = A2T("数据库");
			log.m_sDescriptionl = sErrMsg;
			CLogOper::instance().addMsg(log,m_sGuid);
			messageBox(0, A2T("保存失败"));
		}
		//刷新界面
		messageBox(0, A2T("保存成功"));
		SExecLog sLog;
		sLog.m_nAlarmLevel = enWorkInfo;
		sLog.m_sDescriptionl = sTemplateName;
		/*sLog.m_sDescriptionl = A2T("%1修改了%2名为%3的模版")
			.arg(CPublicData::instance().m_userInfo.sUserName)
			.arg(CPublicData::instance().m_mapIFScene[m_sGuid]->m_sSceneName.get())
			.arg(sTemplateName);*/
		sLog.m_sFuncName = A2T("模版");
		CLogOper::instance().addMsg(sLog,m_sGuid);
		this->close();
	}
}

void CTemplateOperDlg::slotPushButtonAllCheckClicked()
{
	// 遍历并勾选所有二级节点
	for (int i = 0; i < m_pTreeInfoModel->invisibleRootItem()->rowCount(); ++i)
	{
		QStandardItem *item = m_pTreeInfoModel->invisibleRootItem()->child(i);
		for (int j = 0; j < item->rowCount(); j++)
		{
			QStandardItem *pChild = item->child(j);
			// 勾选二级节点
			pChild->setCheckState(Qt::Checked);
		}
	}
}

void CTemplateOperDlg::slotPushButtonDisAllCheckedClicked()
{
	// 遍历并取消勾选所有二级节点
	for (int i = 0; i < m_pTreeInfoModel->invisibleRootItem()->rowCount(); ++i)
	{
		QStandardItem *item = m_pTreeInfoModel->invisibleRootItem()->child(i);
		for (int j = 0; j < item->rowCount(); j++)
		{
			QStandardItem *pChild = item->child(j);
			// 取消勾选二级节点
			pChild->setCheckState(Qt::Unchecked);
		}
	}
}

void CTemplateOperDlg::slotExpandInfo()
{
	ui.treeViewInfo->expandAll();
}

void CTemplateOperDlg::slotShrinkInfo()
{
	ui.treeViewInfo->collapseAll();
}

void CTemplateOperDlg::slotClose()
{
	this->close();
}
